import { TableColumnObject } from 'amis/lib/renderers/Table'
import BaseEnum from '@/core/utils/BaseEnum'
import BaseEnumValue from '@/core/utils/BaseEnumValue'

export type BaseEnumColumnUtilsProp = TableColumnObject & {
	baseEnum?: BaseEnum<any>
	baseEnumValues?: BaseEnumValue<any>[]
}

class BaseEnumColumnUtils {
	getSchema = (parmas: BaseEnumColumnUtilsProp): TableColumnObject => {
		const { name, label, baseEnum, baseEnumValues } = parmas
		const map = {} as any
		if (baseEnum) {
			baseEnum.getAll().forEach(value => {
				map[value.getCode()] = value.getDesc()
			})
		}
		if (baseEnumValues) {
			baseEnumValues.forEach(value => {
				map[value.getCode()] = value.getDesc()
			})
		}
		map['*'] = `\${${name}}`
		const mergeSchema = Object.assign(
			{
				name: name,
				label: label,
				type: 'mapping',
				map: map,
			} as TableColumnObject,
			parmas,
		)

		const schema = mergeSchema as TableColumnObject
		return schema
	}
}

export default new BaseEnumColumnUtils()
